package com.scgk.sync.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.scgk.sync.common.BaseResponse;
import com.scgk.sync.common.ErrorType;
import com.scgk.sync.common.exception.BusinessException;
import com.scgk.sync.common.utils.SftpClientUtil;
import com.scgk.sync.doamin.Flow;
import com.scgk.sync.doamin.ScgkCgRkldYdXzqdqrk;
import com.scgk.sync.service.IFlowService;
import com.scgk.sync.service.IScgkCgRkldYdXzqdqrkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wusong
 * @version 1.0
 * @Description:
 * @create 2024-01-19 9:45
 */
@Api(value = "获取移动数据", tags = "获取移动数据")
@Slf4j
@RestController
@RequestMapping("/yd")
public class GetYdDataController {


    @Autowired
    private IScgkCgRkldYdXzqdqrkService ydXzqdqrkService;
    @Autowired
    private IFlowService iFlowService;

    @ApiOperation("获取行政区常驻人口/月粒度文件数据")
    @PostMapping("/getYdDataByFileName")
    public BaseResponse getYdDataByFileName(String fileName, String FolderName) {
        //// 当天日期 20240121
        //String yyyyMMdd = DateUtil.format(DateUtil.parse(DateUtil.date().toString()), "yyyyMMdd");
        //// 当前小时的前一个小时时间 15
        //String hh = DateUtil.format(DateUtil.offsetHour(DateUtil.parse(DateUtil.date().toString()), -1), "HH");
        String yyyyMMdd = "20240110";
        String hh = "12";
        // 拼装 当前时间的上一个小时文件数据  2024011000
        String synTime = yyyyMMdd + hh;

        FolderName = "20240110";
        fileName = "dwa_ovm_ep_city_12res_hm_" + synTime;
        log.info("---------获取sftp服务器 {} 文件数据------------同步开始-------------------------", fileName);

        List<String> fileList;
        try {
            fileList = ydXzqdqrkService.getYdDataByFileName(fileName, FolderName);

        } catch (Exception e) {
            throw new BusinessException(ErrorType.GET_FILE_FALSE);
        }

        ydXzqdqrkService.addXzqczrk(fileList);

        log.info("---------获取sftp服务器文件数据-------------同步结束-------------------------");
        return BaseResponse.ok();
    }


    @ApiOperation("获取sftp文本流")
    @PostMapping("/getSftpDataIO")
    public BaseResponse getSftpDataIO(String fileName, String FolderName) {
        //// 当天日期 20240121
        //String yyyyMMdd = DateUtil.format(DateUtil.parse(DateUtil.date().toString()), "yyyyMMdd");
        //// 当前小时的前一个小时时间 15
        //String hh = DateUtil.format(DateUtil.offsetHour(DateUtil.parse(DateUtil.date().toString()), -1), "HH");
        String yyyyMMdd = "20240110";
        String hh = "11";
        // 拼装 当前时间的上一个小时文件数据  2024011000
        String synTime = yyyyMMdd + hh;

        FolderName = "20240110";
        fileName = "dwa_ovm_ep_grid_flow_hm_" + synTime;
        log.info("---------获取sftp服务器 {} 文件数据------------同步开始-------------------------", fileName);

        try {
            String format = DateUtil.format(DateUtil.parse(DateUtil.date().toString()), "yyyy-MM-dd");
            // 先判断存不存在
            Flow one = iFlowService.getOne(new LambdaQueryWrapper<Flow>().eq(Flow::getSjd, "2024-01-21").eq(Flow::getHour, "11"));
            if (one != null) {
                //存在
                // 先删除 旧数据
                iFlowService.remove(new LambdaQueryWrapper<Flow>().eq(Flow::getSjd, "2024-01-21").eq(Flow::getHour, "11"));
                //再添加 新的的数据
                Flow flow = iFlowService.getSftpDataIO(fileName, FolderName);
                flow.setSjd(format);
                flow.setHour(hh);
                iFlowService.save(flow);
            } else {
                // 不存在
                Flow flow = iFlowService.getSftpDataIO(fileName, FolderName);
                flow.setSjd(format);
                flow.setHour(hh);
                iFlowService.save(flow);
            }


        } catch (Exception e) {
            throw new BusinessException(ErrorType.GET_FILE_FALSE);
        }


        return BaseResponse.ok();
    }


}
